@echo off
echo ================================
echo    Nginx 服务管理脚本
echo ================================
echo 1. 启动nginx
echo 2. 停止nginx
echo 3. 重启nginx
echo 4. 重载配置
echo 5. 查看nginx状态
echo 6. 查看错误日志
echo 7. 查看访问日志
echo 8. 测试配置文件
echo 9. 退出
echo ================================

set /p choice=请选择操作 (1-9): 

if "%choice%"=="1" goto start_nginx
if "%choice%"=="2" goto stop_nginx
if "%choice%"=="3" goto restart_nginx
if "%choice%"=="4" goto reload_nginx
if "%choice%"=="5" goto status_nginx
if "%choice%"=="6" goto error_log
if "%choice%"=="7" goto access_log
if "%choice%"=="8" goto test_config
if "%choice%"=="9" goto exit_script

echo 无效选择，请重新选择
pause
goto menu

:start_nginx
echo 启动nginx...
call start-nginx.bat
goto menu

:stop_nginx
echo 停止nginx...
call stop-nginx.bat
goto menu

:restart_nginx
echo 重启nginx...
call stop-nginx.bat
timeout /t 2 >nul
call start-nginx.bat
goto menu

:reload_nginx
echo 重载nginx配置...
call reload-nginx.bat
goto menu

:status_nginx
echo nginx运行状态:
tasklist /FI "IMAGENAME eq nginx.exe" 2>NUL | find /I "nginx.exe"
if errorlevel 1 (
    echo nginx未运行
) else (
    echo nginx正在运行
    echo.
    echo 端口监听状态:
    netstat -an | findstr :80
    netstat -an | findstr :443
)
pause
goto menu

:error_log
echo 查看nginx错误日志...
set NGINX_DIR=C:\nginx
if exist "%NGINX_DIR%\logs\error.log" (
    type "%NGINX_DIR%\logs\error.log"
) else (
    echo 错误日志文件不存在
)
pause
goto menu

:access_log
echo 查看nginx访问日志 (最近20行)...
set NGINX_DIR=C:\nginx
if exist "%NGINX_DIR%\logs\access.log" (
    powershell "Get-Content '%NGINX_DIR%\logs\access.log' | Select-Object -Last 20"
) else (
    echo 访问日志文件不存在
)
pause
goto menu

:test_config
echo 测试nginx配置文件...
set NGINX_DIR=C:\nginx
cd /d "%NGINX_DIR%"
nginx.exe -t
pause
goto menu

:exit_script
exit /b 0

:menu
cls
echo ================================
echo    Nginx 服务管理脚本
echo ================================
echo 1. 启动nginx
echo 2. 停止nginx
echo 3. 重启nginx
echo 4. 重载配置
echo 5. 查看nginx状态
echo 6. 查看错误日志
echo 7. 查看访问日志
echo 8. 测试配置文件
echo 9. 退出
echo ================================
set /p choice=请选择操作 (1-9): 
goto start
